﻿/* Copyright ConfigResources.cs Killian Bourrié
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

namespace NTracker.Configs
{
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Linq;

    public class ConfigResources
    {
        private ISettingsManager settingsManager;
        private IDictionary<string, string> dynamicXmlLanguage;
        
        public ConfigResources()
        {
            this.settingsManager = new XmlDocument();
            this.InitObject();
            var settings =  this.PopulateSettings();
            this.PatternsAssemblyFile = settings[C.LIBRARY];
            this.DirectoriesAssemblyFile = settings[C.DIRECTORY];
        }

        public ConfigResources(ISettingsManager settingsManager)
        {
            this.settingsManager = settingsManager;
            this.InitObject();
        }

        public IDictionary<string, string> PatternsAssemblyFile { get; set; }

        public IDictionary<string, string> DirectoriesAssemblyFile { get; set; }

        private void InitObject()
        {
            this.ConfigureDynamicLanguage();
            this.PatternsAssemblyFile = new Dictionary<string, string>();
            this.DirectoriesAssemblyFile = new Dictionary<string, string>();
        }

        private void ConfigureDynamicLanguage() 
        {
            this.dynamicXmlLanguage = new Dictionary<string, string>
            {
                { "$(CurrentDirectory)", Directory.GetCurrentDirectory() }
            };
        }

        private string FilterDynamicXmlLanguage(string xmlValue)
        {
            // Replace special word in XML by code equivalent
            foreach (var specialWord in this.dynamicXmlLanguage)
            {
                xmlValue = xmlValue.Replace(specialWord.Key, specialWord.Value);
            }

            return xmlValue;
        }
        
        private IDictionary<string, string>[] PopulateSettings()
        {
            // Retieve all elements in configuration XML
            IDictionary<string, string>[] settings = new Dictionary<string, string>[] 
            {
                new Dictionary<string, string>(),
                new Dictionary<string, string>()
            };

            // Find path for plugins
            var directories = this.settingsManager.GetSettings()
                .Element("core")
                .Element("directories")
                .Elements("path");

            foreach (var path in directories)
            {
                settings[C.DIRECTORY].Add(
                    path.Attribute("id").Value, 
                    this.FilterDynamicXmlLanguage(path.Attribute("value").Value));
            }

            // Find imports patterns
            var libraries = this.settingsManager.GetSettings()
                .Element("core")
                .Element("libraries")
                .Elements("import");

            foreach (var import in libraries)
            {
                settings[C.LIBRARY].Add(
                    import.Attribute("id").Value,
                    this.FilterDynamicXmlLanguage(import.Attribute("value").Value));
            }

            return settings;
        }
    }
}
